const db = require('../../db')

// 添加校园公告
exports.addSchArt = (req,res)=>{

    const artInfo = {
        // title,content
        ...req.body,
        pubdata:new Date(),
        view:0,
        author:req.user.username,
        aut_id:req.user.id,
        isdelete:0,
        isdown:0     
    }
    var str = "insert into scharticle set ?"
    // 管理员(uid为0的用户)才能发表
    if(req.user.uid == 0 || req.user.uid == 9){
        db.query(str,artInfo,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("发表失败，请重新尝试")
            res.send({
                status:0,
                message:"发表成功"
            })
        })
    }else{
        res.cc("请联系管理员发布")
    }   
}

// 下架校园公告
// 管理员(uid为0或9的用户)才能下架
exports.downSchArt = (req,res)=>{
    var str = "update scharticle set isdown = 1 where id = ?" 
    if(req.user.uid == 0  || req.user.uid == 9){
        db.query(str,req.body.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("下架失败，请重新尝试")
            res.send({
                status:0,
                message:"下架成功"
            })
        })
    }else{
        res.cc("请联系管理员下架")
    } 
}

//恢复下架的校园公告
// 管理员(uid为0或9的用户)才能上架
exports.backDownSchArt = (req,res)=>{
    var str = "update scharticle set isdown = 0 where id = ?" 
    if(req.user.uid == 0  || req.user.uid == 9){
        db.query(str,req.body.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("恢复失败，请重新尝试")
            res.send({
                status:0,
                message:"恢复成功"
            })
        })
    }else{
        res.cc("请联系管理员恢复")
    } 
}




// 假删除校园公告
exports.delSchArt = (req,res)=>{
    var str = "update scharticle set isdelete = 1 where id = ?"
    if(req.user.uid == 0  || req.user.uid == 9){
        db.query(str,req.body.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("删除失败，请重新尝试")
            res.send({
                status:0,
                message:"删除成功"
            })
        })
    }else{
        res.cc("请联系管理员删除")
    }
}

// 恢复假删除

exports.backDelSchArt = (req,res)=>{
    var str = "update scharticle set isdelete = 0 where id = ?"
    if(req.user.uid == 0  || req.user.uid == 9){
        db.query(str,req.body.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("找回失败，请重新尝试")
            res.send({
                status:0,
                message:"找回成功"
            })
        })
    }else{
        res.cc("请联系管理员找回")
    }
}


// 真删除校园公告
exports.rdelSchArt = (req,res)=>{
    var str = "delete from scharticle where id = ?"
    if(req.user.uid == 0  || req.user.uid == 9){
        db.query(str,req.body.id,(err,results)=>{
            if(err) return res.cc(err)
            if(results.affectedRows != 1) return res.cc("删除失败，请重新尝试")
            res.send({
                status:0,
                message:"删除成功"
            })
        })
    }else{
        res.cc("请联系管理员删除")
    }
}